import os
import socket
from utils.manager import Direction


class Settings:
    BASE_DIR = os.path.join(os.path.dirname(os.path.dirname(__file__)), 'snapshot', 'settings')

    ability_name = 'com.ohos.settings.MainAbility'
    bundle_name = 'com.ohos.settings'
    window_name = 'settings'
    call_win = 'callui'
    faceauth_win = 'faceauth'

    search_setting_item = (360, 200)  # 搜索设置项
    wlan_item = (360, 305)  # wlan
    wlan_switch = (640, 210)  # wlan开关
    wlan_pwd_input_area = (360, 200)  # wlan 密码区域
    wlan_pwd_input_show = (640, 200)  # wlan开关
    wlan_cancel_btn = (192, 1142)  # wlan开关
    wlan_connect_btn = (525, 1142)  # wlan开关
    bluetooth_item = (360, 390)  # 蓝牙
    mobile_network_item = (360, 475)  # 移动网络
    display_brightness_item = (360, 590)  # 显示与亮度
    sounds_item = (360, 700)  # 声音
    biometrics_password_item = (360, 815)  # 生物识别和密码
    apps_item = (360, 905)  # 应用
    storage_item = (360, 985)  # 存储
    privacy_item = (360, 1070)  # 隐私
    # 下面的控件需要先往下滑一段距离暴露出来
    accessibility_features_item = (360, 880)  # 辅助功能

    users_accounts_item = (360, 990)  # 用户和帐户
    system_item = (360, 1075)  # 系统
    about_device_item = (360, 1160)  # 关于设备

    lockscreen_pwd = (126, 307)
    lockscreen_pwd_input_area = (333, 258)

    face_auth_page = os.path.join(BASE_DIR, 'face_auth.jpeg')
    mobile_data_page = os.path.join(BASE_DIR, 'mobile_data.jpeg')
    switch_on = os.path.join(BASE_DIR, 'switch_on.jpeg')
    switch_off = os.path.join(BASE_DIR, 'switch_off.jpeg')

    open_wifi = 'A10-3F-Internet'
    encrypt_wifi = 'A10-3F-LAN'
    wifi_pwd = '87654321'

    @classmethod
    def connect_wifi(cls, device):
        device.go_home()
        # 打开设置
        device.force_stop(cls.bundle_name)
        device.start_ability(cls.bundle_name, cls.ability_name)
        device.wait(1)
        device.dirc_fling(Direction.Up)
        device.click(*cls.wlan_item)
        device.wait(3)
        device.refresh_layout()
        if device.get_element_by_text('已连接WLAN'):
            device.go_back()
            device.go_home()
            return True
        # 检查wifi开关状态
        toggle = device.get_element_by_type('Toggle')
        if toggle.get('checked') == 'false':
            device.click_element(toggle)
        device.wait(10)
        device.refresh_layout()
        if device.get_element_by_text('已连接WLAN'):
            device.go_back()
            device.go_home()
            return True
        wifi = device.get_element_by_text(cls.open_wifi)
        if not wifi:
            device.go_back()
            device.go_home()
            return False
        device.click_element(wifi)

        device.wait(10)
        device.refresh_layout()
        if device.get_element_by_text('已连接WLAN'):
            device.go_back()
            device.go_home()
            return True
        device.go_back()
        device.go_home()
        return False

    @classmethod
    def bluetooth(cls):
        with open('D:\\DeviceTestTools\\reference.txt', 'r') as f:
            sn = f.read().strip()
        hostname = socket.gethostname()
        # name为本机的IP地址
        name = socket.gethostbyname(hostname)
        return {
            'sn': sn,
            'name': name
        }

    @classmethod
    def open_bluetooth_switch(cls, device):
        device.wakeup()
        device.unlock()
        device.go_home()
        device.set_screen_timeout(300)
        device.force_stop(cls.bundle_name)
        device.start_ability(cls.bundle_name, cls.ability_name)
        device.wait(1)
        device.dirc_fling(Direction.Up)
        device.click(*cls.bluetooth_item)
        device.refresh_layout()
        # 检查wifi开关状态
        toggle = device.get_element_by_type('Toggle')
        if toggle.get('checked') == 'false':
            device.click_element(toggle)
